﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using WinFormsApp1.Leo;

namespace WinFormsApp1
{
    /// <summary>
    /// 客户管理添加
    /// </summary>
    public partial class Clientser : Form
    {
        public Clientser()
        {
            InitializeComponent();
            GetPoe();
            GetSrction();
        }

        //部门
        public async void GetSrction()
        {
            //var data = new
            //{

            //};

            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<Sectionstes>>>(URLCommon.Url2, "/Week01Get/ShowSectionData1", HttpClientHelper.MethodType.GET, default);

            comboSections.DataSource = items.Data;
            comboSections.DisplayMember = "businessName";
            comboSections.ValueMember = "businessId";

            this.Controls.Add(comboSections);
        }

        //省
        public async void GetPoe()
        {
            Provibces provibces = new();

            var items = await HttpClientHelper.RequestUrl<Provibces, ApiResult<List<Provibcessend>>>(URLCommon.Url2, "/Week01Get/ProvibcesShow", HttpClientHelper.MethodType.POST, provibces);

            comboProvibce.DataSource = items.Data;
            comboProvibce.DisplayMember = "ProvibceName";
            comboProvibce.ValueMember = "ProvibceId";

            this.Controls.Add(comboProvibce);
        }

        public record Clients(string customerNickname, string customerIDcar, int provibceId, int cityId, int areaId, DateTime customerCooperateTime
            , int sectionId, bool customerState, string customerNotes, bool isDelete);
        



        public record Provibces();


        public record Citys(int provibceId);
        //省点击事件
        private async void comboProvibce_Click(object sender, EventArgs e)
        {
            var provibceId = Convert.ToInt32(comboProvibce.SelectedValue);

            Citys citys = new(provibceId);

            var items1 = await HttpClientHelper.RequestUrl<Citys, ApiResult<List<CitysTes>>>(URLCommon.Url2, "/WeekListOne_Leo/GetCitys", HttpClientHelper.MethodType.POST, citys);

            comboCity.DataSource = items1.Data;
            comboCity.DisplayMember = "CitysName";
            comboCity.ValueMember = "CityId";


            this.Controls.Add(comboCity);
        }


        public record Areas(int cityId);

        //区点击事件
        private async void comboCity_Click(object sender, EventArgs e)
        {
            var cityId = Convert.ToInt32(comboCity.SelectedValue);

            Areas areas = new(cityId);

            var items2 = await HttpClientHelper.RequestUrl<Areas, ApiResult<List<Areastes>>>(URLCommon.Url2, "/WeekListOne_Leo/GetAreas", HttpClientHelper.MethodType.POST, areas);

            comboArea.DataSource = items2.Data;
            comboArea.DisplayMember = "AreaName";
            comboArea.ValueMember = "AreaId";


            this.Controls.Add(comboArea);
        }
        //添加
        private async void button1_Click_1(object sender, EventArgs e)
        {
            var customerNickname = textNickname.Text;
            var customerIDcar = textIDcar.Text;
            var provibceId = Convert.ToInt32(comboProvibce.SelectedValue);
            var cityId = Convert.ToInt32(comboCity.SelectedValue);
            var areaId = Convert.ToInt32(comboArea.SelectedValue);
            var customerCooperateTime = Convert.ToDateTime(dateTimeCooper.Text);
            var sectionId = Convert.ToInt32(comboSections.SelectedValue);
            var customerState = false;
            var customerNotes = richNotes.Text;
            var isDelete = false;


            Clients clients = new(customerNickname, customerIDcar, provibceId, cityId, areaId, customerCooperateTime, sectionId, customerState
                , customerNotes, isDelete);
            var res = await HttpClientHelper.RequestUrl<Clients, ApiResult<Clientstes>>(URLCommon.Url1, "/Week01/GetClients", HttpClientHelper.MethodType.POST, clients);
            if (res.Code == 1)
            {
                MessageBox.Show("添加成功");
            }
        }
    }
}
